// This function will display all buildingpos and buildingexits in a given area.
XfMissionShowAllBuildingPos = {
	private ["_center","_radius","_building_pos","_i","_marker_name","_placer"];
	_center = _this select 0;
	_radius = _this select 1;
	
	_building_pos = [_center,_radius] call XfMissionGetNearestBuildingPositions;
	_i = 0;
	{
		_marker_name = format["building_pos_%1",_i];
		[_marker_name, _x, "ICON", "ColorRed", [0.5,0.5],str(round(_x select 2)),0,"mil_dot","",1] call XfCreateMarkerGlobal;
		_i = _i + 1;
		_placer = createVehicle ["EvKobalt", _x, [], 0, "CAN_COLLIDE"];
	} foreach _building_pos;
	_building_pos = [_center,_radius] call XfMissionGetNearestBuildingEntrances;
	_i = 0;
	{
		_marker_name = format["building_exit_%1",_i];
		[_marker_name, _x, "ICON", "ColorRed", [0.5,0.5],str(round(_x select 2)),0,"mil_triangle","",1] call XfCreateMarkerGlobal;
		_i = _i + 1;
		_placer = createVehicle ["Radio", _x, [], 0, "CAN_COLLIDE"];
	} foreach _building_pos;
};

// Returns an array with all available positions inside a building.
XfMissionGetBuildingPos = {
	private ["_index","_array","_noPosition","_foundPosition"];
	_noPosition = false;
	_bld = _this;
	_type = "INTERN";
	if (typeName _this == "ARRAY") then {
		_bld = _this select 0;
		if (count _this > 1) then { _type = _this select 1;};
	};
	_index = 0;
	_array = [];
	if (!isNil "XfBuildingPosArray") then {
		// Using homebrewn building pos
		_bldarr = [_bld,_type,True] call XfBuildingPosArray;
		If (count _bldarr > 0) then {
			{ _bldgpos set [count _bldgpos,_x];	
			} foreach _bldarr;
		};
	} else {
		While {!_noPosition} do {
			_foundPosition = _bld buildingPos _index;
			if ((_foundPosition select 0 != 0) || (_foundPosition select 0 != 0)) then {
				_array set [count _array, _foundPosition];
			} else {
				_noPosition = true;
			};
			_index = _index + 1;
		};
	};
	_array;
};

// Returns an array with all available exits of a building.
XfMissionGetBuildingExits = {
	private ["_index","_array","_noPosition","_foundPosition"];
	_noPosition = false;
	_index = 0;
	_array = [];
	While {!_noPosition} do {
		_foundPosition = _this buildingExit _index;
		if ((_foundPosition select 0 != 0) || (_foundPosition select 0 != 0)) then {
			_array set [count _array, _foundPosition];
		} else {
			_noPosition = true;
		};
		_index = _index + 1;
	};
	_array;
};

// This functions looks around the given position if there are buildings.
// Then it looks if those buildings contain Building Positions for NPCs.
XfMissionGetNearestBuildingPositions = {
	private ["_center","_radius","_nearestBuildings","_buildingPositions","_currentBuildingPositions"];
	_center = _this select 0;
	_radius = _this select 1;
	_nearestBuildings = [_center select 0,_center select 1] nearObjects ["House",_radius];
	_buildingPositions = [];
	If (count _nearestBuildings > 0) then {
		{
			_currentBuildingPositions = _x call XfMissionGetBuildingPos;
			if (count _currentBuildingPositions > 0) then {
				_buildingPositions = [_buildingPositions, _currentBuildingPositions] call XfArrayPushStack;
			};
		} foreach _nearestBuildings;
	};
	_buildingPositions;
};

// This functions looks around the given position if there are buildings.
// Then it looks if those buildings contain Building Entrances.
XfMissionGetNearestBuildingEntrances = {
	private ["_center","_radius","_nearestBuildings","_buildingExits","_currentBuildingExits"];
	_center = _this select 0;
	_radius = _this select 1;
	_nearestBuildings = [_center select 0,_center select 1] nearObjects ["House",_radius];
	_buildingExits = [];
	If (count _nearestBuildings > 0) then {
		{
			_currentBuildingExits = _x call XfMissionGetBuildingExits;
			if (count _currentBuildingExits > 0) then {
				_buildingExits = [_buildingExits, _currentBuildingExits] call XfArrayPushStack;
			};
		} foreach _nearestBuildings;
	};
	_buildingExits;
};

XfMissionLoadAllGroups = {
	if (isNil "MISSION_ARRAY_GROUP_NAME") then {
		MISSION_ARRAY_GROUP_NAME = [];
	};
	_nb = count (configFile >> "CfgGroups");
	for [{_i = 0}, {_i < _nb}, {_i = _i + 1}] do {
		if (isClass ((configFile >> "CfgGroups") select _i)) then {
			_entry_side = ((configFile >> "CfgGroups") select _i);
			_side_name = configName _entry_side;
			_side_displayName = getText(_entry_side >> "name");
			_nb_factions = count _entry_side;
			for [{_z = 0}, {_z < _nb_factions}, {_z = _z + 1}] do {
				if (isClass (_entry_side select _z)) then {
					_entry_faction = (_entry_side select _z);
					_faction_name = configName _entry_faction;
					_faction_displayName = getText(_entry_faction >> "name");
					_nb_groups = count _entry_faction;
					for [{_x = 0}, {_x < _nb_groups}, {_x = _x + 1}] do {
						if (isClass (_entry_faction select _x)) then {
							_entry_group = (_entry_faction select _x);
							if (isClass (_entry_group)) then {
								_group_name = configName _entry_group;
								_group_displayName = getText(_entry_group >> "name");
								_nb_child = count _entry_group;
								for [{_y = 0}, {_y < _nb_child}, {_y = _y + 1}] do {
									if (isClass (_entry_group select _y)) then {
										_entry_u_group = (_entry_group select _y);
										_u_group_name = configName _entry_u_group;
										_u_group_displayName = getText(_entry_u_group >> "name");
										_arr2 = [];
										_arr2 set [0, toUpper(_side_name)];
										_arr2 set [1, _side_name];
										_arr2 set [2, _faction_name];
										_arr2 set [3, _group_name];
										_arr2 set [4, _u_group_name];
										_arr2 set [5, _u_group_displayName];
										MISSION_ARRAY_GROUP_NAME set [count MISSION_ARRAY_GROUP_NAME,_arr2];
									};
								};
							};
						};
					};
				};
			};
		};
	};
};